<?php
/*********************************************************************\
* This File is a part of BH-PANEL (Breizh-Heberg Panel v2.1)
* Copyright (C) 2008-2009 the BH Developpers : Vincent Giersch <mail@vincordi.fr>, Cyprien Laleau <fanning.fr@gmail.com> and Edwin Cabiten <marmottes44@hotmail.fr>
* See file AUTHORS to get more informations

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
/*********************************************************************/

if(isset($_SESSION['u_nic'])&&isset($_GET['id'])) {
	include('php/lib/panel_inc.php');
	$dom_current = intval($_GET['id']);
	$infos_dom = query("SELECT 
								comptes_domain.*,
								users.reg_contact_id,
								users.u_nic
								FROM comptes_domain
								LEFT JOIN users
								ON comptes_domain.u_nic = users.u_nic
								WHERE comptes_domain.id='".$dom_current."' AND comptes_domain.u_nic='".$_SESSION['u_nic']."'");
	if(mysql_num_rows($infos_dom) == 1) {
		$infos_dom = mysql_fetch_array($infos_dom);
		if($infos_dom['etat'] == "1") {
				// Création d'une Zone DNS
				if($_GET['action'] == "on") {
					$exist = mysql_fetch_array(query("SELECT COUNT(*) AS exist FROM zones_dns WHERE id_domain = '$dom_current'"));
					if($exist['exist'] == 0) {
						query("INSERT INTO zones_dns(id_domain) VALUES('$dom_current')");
						$id_zone = mysql_fetch_array(query("SELECT id FROM zones_dns WHERE id_domain = '$dom_current'"));
						// Class Dom BH
						include ROOT.'php/lib/registrars/'.$registrar[$infos_dom['id_registrar']].'.inc.php';
						$_domgest = new bh_doms_gest();
						$dom_info = $_domgest->dom_infos($infos_dom['domaine']);	
						$_domgest->deconnect();
						$update['dns1'] = "ns2.breizh-heberg.com";
						$update['dns2'] = "ns3.breizh-heberg.com";
						$update['dns3'] = "";
						$update['dns4'] = "";
						$update['reg_contact_id'] = $infos_dom['reg_contact_id'];
						if($infos_dom['lock'] == 1) {
							$update['registry_status'] = 64;
						}
						else {
							$update['registry_status'] = 0;
						}
						if(($infos_dom['dns1'] == "ns2.breizh-heberg.com") AND ($infos_dom['dns2'] == "ns3.breizh-heberg.com")) {
							query("UPDATE comptes_domain SET zone_dns = '".$id_zone['id']."', dns1='ns0.breizh-heberg.com', dns2='ns3.breizh-heberg.com', dns3='', dns4='' WHERE id = '$dom_current'");
						}
						else{
							$tid = $_domgest->dom_update($infos_dom['domaine'],$update);
							query("UPDATE comptes_domain SET zone_dns = '".$id_zone['id']."', dns1='ns0.breizh-heberg.com', dns2='ns3.breizh-heberg.com', dns3='', dns4='', tid='$tid', etat = '7' WHERE id = '$dom_current'");
						}
						
					}
					header("Location:domaine.html?id=".$dom_current);
				}
				// SUppression d'une zone DNS
				elseif($_GET['action'] == "off") {
					$id_zone = mysql_fetch_array(query("SELECT id FROM zones_dns WHERE id_domain = '$dom_current'"));
					query("DELETE FROM zones_dns_content WHERE id_zone = '".$id_zone['id']."'");
					query("DELETE FROM zones_dns WHERE id = '".$id_zone['id']."'");
					query("UPDATE comptes_domain SET zone_dns = '0' WHERE id = '$dom_current'");
					header("Location:domaine.html?id=".$dom_current);
				}
				// Modification de la zone DNS
				elseif($_GET['action'] == "modif") {
					if($infos_dom['zone_dns'] != 0) {
						//Ajouter une entrée
						if($_GET['zd'] == "add") {
							if(isset($_POST['cible'])&&isset($_POST['type'])) {
								if($_POST['type_cible'] == 'host') {
									$host = preg_replace('#([^a-z0-9\-\_.])+#i', '', strtolower($_POST['cible']));
								}
								else {
									$host = intval($_POST['ip_1']).'.'.intval($_POST['ip_2']).'.'.intval($_POST['ip_3']).'.'.intval($_POST['ip_4']);
								}
								$entree_value = preg_replace('#([^a-z0-9.\-\_*@])+#i', '', strtolower($_POST['entree_value']));
								$id_zone = mysql_fetch_array(query("SELECT zone_dns FROM comptes_domain WHERE id='".intval($_GET['id'])."' AND u_nic = '".$_SESSION['u_nic']."'"));
								$verif_exist = mysql_fetch_array(query("SELECT COUNT(*) AS verif FROM zones_dns_content WHERE entree = '$entre_value' AND id_zone = '".$id_zone['id']."'"));
								$type = preg_replace('#([^A-Z])+#i', '', $_POST['type']);
								if($type == "MX") {
									if(isset($_POST['mx_value'])) {
										$mx_value = intval($_POST['mx_value']);
									}
									else {
										$mx_value = 1;
									}
								}
								$zone = $id_zone['zone_dns'];
								if($verif_exist['verif'] == 0) {
									query("INSERT INTO zones_dns_content VALUE('', '$entree_value', '$type', '$mx_value', '$host', '$zone',0)");
									query("UPDATE zones_dns SET etat = 0  WHERE id = '$zone'");
									header("Location:domaine-zone-dns.html?action=modif&id=".$dom_current);
								}
								else {
									header("Location:domaine-zone-dns.html?action=modif&id=".$dom_current);
								}
							}
							else {
								$title = "Ajouter une entrée DNS";
								$tplinc = "panel-domaine-zdns-entree-add";
								$parse = 1;
								$tpl->assign(array(
									'iconesleft' => 3,
									'prod' => '1',
									'prod_id' => intval($_GET['id']),
									'current_dom' => $infos_dom['domaine'],
									'zone_dns' => $infos_dom['zone_dns']));
							}
						}
						// Modifier une entrée
						elseif($_GET['zd'] == "mod") {
							if(isset($_POST['cible'])) {
								$id_entree = intval($_POST['id_entree']);
								$host = preg_replace('#([^a-z0-9\-\_.])+#i', '', strtolower($_POST['cible']));
								$verif = query("SELECT
												comptes_domain.u_nic,
												comptes_domain.zone_dns,
												zones_dns_content.id
												FROM zones_dns_content
												LEFT JOIN comptes_domain
												ON zones_dns_content.id_zone=comptes_domain.zone_dns
												WHERE zones_dns_content.id = '$id_entree' AND comptes_domain.u_nic='".$_SESSION['u_nic']."'");
								if(mysql_num_rows($verif) == 1) {
									query("UPDATE zones_dns_content SET value = '$host' WHERE id='$id_entree'");
									query("UPDATE zones_dns SET etat = 0  WHERE id = '$zone'");
									header("Location:domaine-zone-dns.html?action=modif&id=".$dom_current);
								}
								else {
									header("Location:domaine-zone-dns.html?action=modif&id=".$dom_current);
								}
							}
							else {
								$title = "Modifier une entrée DNS";
								$tplinc = "panel-domaine-zdns-entree-mod";
								$parse = 1;
								$tpl->assign(array(
										'iconesleft' => 3,
										'prod' => '1',
										'prod_id' => intval($_GET['id']),
										'current_dom' => $infos_dom['domaine'],
										'zone_dns' => $infos_dom['zone_dns']));
								$entrees = query("SELECT id, entree, type, value_mx FROM zones_dns_content WHERE id_zone = '".$infos_dom['zone_dns']."'  AND lie_heberg='0' ORDER by entree");
								While($liste_entrees = mysql_fetch_array($entrees)) {
									$tpl->assign_array('entrees', array(
										'id_entree' => $liste_entrees['id'],
										'entree' => $liste_entrees['entree'],
										'type' => $liste_entrees['type'].' '.$liste_entrees['value_mx']
									));
								}
							}
						}
						// Supprimer une entrée
						elseif($_GET['zd'] == "del") {
								if(isset($_POST['entree'])) {
									$id_entree = intval($_POST['entree']);
									$verif = query("SELECT
													comptes_domain.u_nic,
													comptes_domain.zone_dns,
													zones_dns_content.id
													FROM zones_dns_content
													LEFT JOIN comptes_domain
													ON zones_dns_content.id_zone=comptes_domain.zone_dns
													WHERE zones_dns_content.id = '$id_entree' AND comptes_domain.u_nic='".$_SESSION['u_nic']."'");
									if(mysql_num_rows($verif) == 1) {
										query("DELETE FROM zones_dns_content WHERE id='$id_entree'");
										query("UPDATE zones_dns SET etat = 0  WHERE id = '$zone'");
										header("Location:domaine-zone-dns.html?action=modif&id=".$dom_current);
									}
								}
								else {
									$title = "Supprimer une entrée DNS";
									$tplinc = "panel-domaine-zdns-entree-del";
									$parse = 1;
									$tpl->assign(array(
											'iconesleft' => 3,
											'prod' => '1',
											'prod_id' => intval($_GET['id']),
											'current_dom' => $infos_dom['domaine'],
											'zone_dns' => $infos_dom['zone_dns']));
									$entrees = query("SELECT id, entree, type, value_mx FROM zones_dns_content WHERE id_zone = '".$infos_dom['zone_dns']."' AND lie_heberg = '0' ORDER by entree");
									While($liste_entrees = mysql_fetch_array($entrees)) {
										$tpl->assign_array('entrees', array(
											'id_entree' => $liste_entrees['id'],
											'entree' => $liste_entrees['entree'],
											'type' => $liste_entrees['type'].' '.$liste_entrees['value_mx']
										));
									}
								}
						
						}
						else {
							$title = "Modifier une zone DNS";
							$tplinc = "panel-domaine-zdns-modif";
							$zones_dns = query("SELECT 
												zones_dns.id,
												zones_dns.id_domain,
												zones_dns_content.entree,
												zones_dns_content.type,
												zones_dns_content.value,
												zones_dns_content.value_mx,
												zones_dns_content.id_zone,
												zones_dns_content.id AS entree_id
												FROM zones_dns
												LEFT JOIN zones_dns_content
												ON zones_dns_content.id_zone = zones_dns.id
												WHERE zones_dns.id_domain = '".$dom_current."' AND zones_dns_content.id != ''
												ORDER by zones_dns_content.entree
												");
							$lie = mysql_fetch_array(query("SELECT COUNT(*) AS lie FROM hebergdata_doms WHERE id='".$dom_current."'"));
							$tpl->assign(array('lie' => $lie['lie']));
							if($lie['lie'] == 1) {
								$tpl->assign(array(
								"load" => "new Effect.Pulsate(document.getElementById('lie'))"
								));
							}
							if(mysql_num_rows($zones_dns) != 0) {
								$tpl->assign(array('zone1' => 1));
							}
							While($zone_d = mysql_fetch_array($zones_dns)) {
								$tpl->assign_array('zonedns', array(
									'entree' => $zone_d['entree'],
									'type' => $zone_d['type'],
									'value' => $zone_d['value'],
									'entree_id' => $zone_d['entree_id'],
									'value_mx' => $zone_d['value_mx'],
								));
							}
							$tpl->assign(array(
								'iconesleft' => 3,
								'prod' => '1',
								'prod_id' => intval($_GET['id']),
								'current_dom' => $infos_dom['domaine'],
								'ns_prim' => $bzh_config['ns_prim'],
								'ns_sec' => $bzh_config['ns_sec'],
								'zone_dns' => $infos_dom['zone_dns'],
								));
							$parse = 1;
						}
					}
					else {
						header("Location:domaine.html?id=".$dom_current);
					}
				}
		}
		else { header("Location:/panel/domaine.html?id=".intval($_GET['id'])); }
	}
}
else { header("Location:/site/connexion.html"); }

?>
